<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/%E6%AD%A6%E6%B1%8932x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/%E6%AD%A6%E6%B1%8916x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib1/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib1/pace/pace-theme-center-simple.min.css">
  <script src="/lib1/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="1、基础知识NDArray、NumPy的广播机制：数组维度不同，后缘维度的轴长（从末尾算起的维度）相同；（4，3）+（3，）；（3，4，2）+（4，2）   2、数组维度相同，其中有个轴为1；（4，3）+（4，1）：在1轴上广播扩展。 NDArray,NumPy的相互变换： 123P &#x3D; np.ones((2,3))D &#x3D; nd.array(P)D.asnumpy()  自动求梯度（gradien">
<meta property="og:type" content="article">
<meta property="og:title" content="MXNET机器学习初见">
<meta property="og:url" content="http://example.com/2020/08/19/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E5%88%9D%E8%A7%81/index.html">
<meta property="og:site_name" content="Technological Blog">
<meta property="og:description" content="1、基础知识NDArray、NumPy的广播机制：数组维度不同，后缘维度的轴长（从末尾算起的维度）相同；（4，3）+（3，）；（3，4，2）+（4，2）   2、数组维度相同，其中有个轴为1；（4，3）+（4，1）：在1轴上广播扩展。 NDArray,NumPy的相互变换： 123P &#x3D; np.ones((2,3))D &#x3D; nd.array(P)D.asnumpy()  自动求梯度（gradien">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-08-19T15:28:46.000Z">
<meta property="article:modified_time" content="2021-11-03T08:23:24.634Z">
<meta property="article:author" content="Li Yudong">
<meta property="article:tag" content="机器学习初学">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/2020/08/19/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E5%88%9D%E8%A7%81/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>MXNET机器学习初见 | Technological Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Technological Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">IT小白的成长之旅</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="tags fa-fw"></i>标签<span class="badge">18</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="th fa-fw"></i>分类<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="archive fa-fw"></i>归档<span class="badge">95</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/tiarmor1" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2020/08/19/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E5%88%9D%E8%A7%81/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/author.jpg">
      <meta itemprop="name" content="Li Yudong">
      <meta itemprop="description" content="">
    </span>
    
    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Technological Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          MXNET机器学习初见
        </h1>
    
        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
    
              <time title="创建时间：2020-08-19 23:28:46" itemprop="dateCreated datePublished" datetime="2020-08-19T23:28:46+08:00">2020-08-19</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-11-03 16:23:24" itemprop="dateModified" datetime="2021-11-03T16:23:24+08:00">2021-11-03</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/" itemprop="url" rel="index"><span itemprop="name">机器学习</span></a>
                </span>
            </span>
    
          
    
        </div>
      </header>
    
    
    
    
    <div class="post-body" itemprop="articleBody">
    
      
        <h2 id="1、基础知识"><a href="#1、基础知识" class="headerlink" title="1、基础知识"></a>1、基础知识</h2><p>NDArray、NumPy的广播机制：数组维度不同，后缘维度的轴长（从末尾算起的维度）相同；（4，3）+（3，）；（3，4，2）+（4，2）   2、数组维度相同，其中有个轴为1；（4，3）+（4，1）：在1轴上广播扩展。</p>
<p>NDArray,NumPy的相互变换：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">P = np.ones((<span class="number">2</span>,<span class="number">3</span>))</span><br><span class="line">D = nd.array(P)</span><br><span class="line">D.asnumpy()</span><br></pre></td></tr></table></figure>

<p>自动求梯度（gradient）MXNET中使用autograd模块自动求梯度：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">x = nd.arange(<span class="number">4</span>).reshape((<span class="number">4</span>,<span class="number">1</span>))</span><br><span class="line">x.attach_grad()  <span class="comment">#申请内存</span></span><br><span class="line"><span class="keyword">with</span> autograd.record():</span><br><span class="line">    y = <span class="number">2</span> * nd.dot(x.T,x) <span class="comment">#若y为标量，贼会默认对y元素求和后，求关于X的梯度</span></span><br><span class="line">y.backward()</span><br></pre></td></tr></table></figure>



<p>uniform:均匀分布采样；normal:正态分布采样；poisson:泊松分布采样。</p>
<h2 id="2、线性回归"><a href="#2、线性回归" class="headerlink" title="2、线性回归"></a>2、线性回归</h2><p>损失函数：平方函数，平方损失；在模型训练中，希望找到一组模型参数为w1,w2,b使得训练样本平均损失最小。</p>
<p>解析解：误差最小化问题的解刚好可用数学公式表达出来；大多数为数值解，只能利用优化算法有限次迭代模型参数，从而尽可能降低损失函数的值。</p>
<p>全连接层：又名稠密层，输出层中的神经元与输入层中的各个输入完全连接；</p>
<p>矢量计算比标量逐个相加更加省时间，故往往利用矢量矩阵运算来实现深度学习；</p>
<p>优化算法：小批量随机梯度下降：批量大小batch size,学习率 lr 均为超参数，为人为设定并非模型学习出来的，</p>
<p>调参：通过反复试错来寻找合适的超参数，</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">sgd</span><span class="params">(params,lr,batch_size)</span>:</span> <span class="comment">#sgd函数实现小批量随机梯度下降算法</span></span><br><span class="line">  <span class="keyword">for</span> param <span class="keyword">in</span> params:</span><br><span class="line">    param[:] = param - lr * param.grad / batch_size</span><br></pre></td></tr></table></figure>

<p>在一个迭代周期epoch中，将完整遍历一遍data_iter函数，并对训练数据集中所有样本都使用一次。</p>
<h3 id="Gluon简洁实现："><a href="#Gluon简洁实现：" class="headerlink" title="Gluon简洁实现："></a>Gluon简洁实现：</h3><p>1、提供data包来读取数据：</p>
<p>2、提供大量预定义层，nn模块：neural networks：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> mxnet.gluon <span class="keyword">import</span> nn</span><br><span class="line">net = nn.Sequential()  <span class="comment">#Sequential实例是串联各层的容器，依次添加层，每一层一次计算并作为下一层输入</span></span><br><span class="line">net.add(nn.Dense(<span class="number">1</span>)) <span class="comment">#Dense全连接层，GLUON无须指定各层形状，模型会自动推断</span></span><br></pre></td></tr></table></figure>

<p>3、利用init模块来实现模型参数初始化的各种方法：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> mxnet.gluon <span class="keyword">import</span> init</span><br><span class="line">net.initialize(init.Normal(sigma=<span class="number">0.01</span>)) <span class="comment">#均值为0，标准差0.01的正态分布</span></span><br></pre></td></tr></table></figure>

<p>4、定义损失函数：利用loss模块：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> mxnet.gluon <span class="keyword">import</span> loss <span class="keyword">as</span> gloss</span><br><span class="line">loss = gloss.L2Loss() <span class="comment">#平方损失又是L2范数损失</span></span><br></pre></td></tr></table></figure>

<p>5、定义优化算法：创建Trainer实例，以sgd作为优化算法，用来迭代net实例所有通过add函数嵌套的层包含的全部参数，可通过collect_params函数获取。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">trainer = gluon.Trainer(net.collect_params(),<span class="string">'sgd'</span>,&#123;<span class="string">'learning_rate'</span>:<span class="number">0.03</span>&#125;)</span><br></pre></td></tr></table></figure>

<p>6、训练模型：调用Trainer实例的step函数来迭代模型参数，按sgd的定义，在step中指明批量大小，从而对样本梯度求平均。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">num_epochs = <span class="number">3</span></span><br><span class="line"><span class="keyword">for</span> epoch <span class="keyword">in</span> range(<span class="number">1</span>,num_epochs + <span class="number">1</span>):</span><br><span class="line">    <span class="keyword">for</span> X,y <span class="keyword">in</span> data_iter:</span><br><span class="line">        <span class="keyword">with</span> autograd.record():</span><br><span class="line">            l = loss(net(X),y)</span><br><span class="line">        l.backward()</span><br><span class="line">        trainer.step(batch_size)</span><br><span class="line">    l = loss(net(features), labels)</span><br><span class="line">    print(<span class="string">'epoach %d,loss: %f'</span>%(epoch,l.mean().asnumpy()))</span><br></pre></td></tr></table></figure>



<h2 id="3、softmax回归"><a href="#3、softmax回归" class="headerlink" title="3、softmax回归"></a>3、softmax回归</h2><p>模型输出为图像类别这种离散值时，使用softmax回归，其输出单元从一个变成了多个，且引入了softmax运算使输出更加适合离散值的预测和训练。</p>
<p>sofemax回归模型：将输出特征与权重做线性叠加，输出值个数等于标签里的类别数。例：有4种特征（4个像素的图片）和3种输出动物类别，则权重包含12个标量（带下标w)、偏差包含3个标量（带下标b）。每个On计算都依赖所有输入，故为全连接层。</p>
<p>softmax计算：直接用最高On作为预测输出，有2个问题。1、输出值范围不定，难以直观判断；2、误差难以衡量。softmax运算符可以解决，即归一化,但softmax运算不改变预测类别输出。<br>$$<br>y1 = exp(O1)/exp(O1)+exp(O2)+exp(O3)<br>$$<br>交叉熵函数：使用更适合衡量分布差异的测量函数，只关心对正确类别的预测概率，<br>$$<br>H(yi,y<em>i) = -Σ(yilogy</em>i)<br>$$<br>交叉熵损失函数，最小化其等价于最大化训练数据集所有标签类别的联合预测概率。<br>$$<br>l(o) =1/n * Σ(yilogy*i)<br>$$<br>图像分类数据集：Fashion-MNIST</p>
<p>Gluon的DataLoader中可用多进程来加速数据读取；通过ToTensor实例将图像数据从unit8格式变换成32位浮点数格式，并除以255使得所有像素均在0至1之间。</p>
<h3 id="Gluon简洁实现"><a href="#Gluon简洁实现" class="headerlink" title="Gluon简洁实现"></a>Gluon简洁实现</h3><p>1、导入模块并获取函数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">%matplotlib inline</span><br><span class="line"><span class="keyword">import</span> d2zlzh <span class="keyword">as</span> d2l</span><br><span class="line"><span class="keyword">from</span> mxnet <span class="keyword">import</span> gluon, init</span><br><span class="line"><span class="keyword">from</span> mxnet.gluon <span class="keyword">import</span> loss <span class="keyword">as</span> gloss, nn</span><br><span class="line">batch_size = <span class="number">256</span> <span class="comment">#批量大小设置</span></span><br><span class="line">train_iter,test_iter = d2l.load_data_fashion_mnist(batch_size)</span><br></pre></td></tr></table></figure>

<p>2、定义和初始化模型</p>
<p>添加输出为10的全连接层，并用均值为0、标准差为0.01的正态分布随机初始化模型的权重参数。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">net = nn.Sequential()</span><br><span class="line">net.add(nn.Dense(<span class="number">10</span>))</span><br><span class="line">net.initialize(init.Normal(sigma=<span class="number">0.01</span>))</span><br></pre></td></tr></table></figure>

<p>3、同时定义softmax和交叉熵损失函数，使数值稳定性更好，使用Gluon提供的函数。</p>
<p>定义优化算法：使用学习率为0.1的小批量随机梯度下降算法。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">loss = gloss.SoftmaxCrossEntropyLoss()</span><br><span class="line">trainer = gluon.Trainer(net.collect_params()，<span class="string">'sgd'</span>,&#123;<span class="string">'learining_rate'</span>:<span class="number">0.1</span>&#125;)</span><br></pre></td></tr></table></figure>

<p>4、使用上一节定义的训练函数来训练模型：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">num_epochs = <span class="number">5</span></span><br><span class="line">d2l.train_ch3(net,train_iter,test_iter,loss,num_epochs,batch_size,<span class="literal">None</span>,<span class="literal">None</span>,trainer)</span><br></pre></td></tr></table></figure>



<h2 id="4、多层感知机"><a href="#4、多层感知机" class="headerlink" title="4、多层感知机"></a>4、多层感知机</h2><p>深度学习主要关注多层模型，以多层感知机NLP（multilayer perceptron）为例。在单层网络的基础上引入了隐藏层hidden layer，但多个仿射线性变换叠加仍然是线性仿射，需引入非线性函数，该函数被称为激活函数</p>
<p>RELU函数：RELU(x) = max(x,0)</p>
<p>sigmoid函数：sigmoid(x) = 1/[1+exp(-x)]</p>
<p>sigmoid函数的导数：sigmoid(x)(1-sigmoid(x))</p>
<p>tanh（双曲正切）函数：[1- exp(-2x)]/[1+exp(-2x)]</p>
<p>tanh函数的导数：1 - [tanh(x)]^2</p>
<h3 id="Gluon的简洁实现"><a href="#Gluon的简洁实现" class="headerlink" title="Gluon的简洁实现"></a>Gluon的简洁实现</h3><p>1、导入包与模块，并定义模型,，多加一个全连接作为隐藏层，单元数为256，用RELU作为激活函数。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> d2zlzh <span class="keyword">as</span> d2l</span><br><span class="line"><span class="keyword">from</span> mxnet <span class="keyword">import</span> gluon, init</span><br><span class="line"><span class="keyword">from</span> mxnet.gluon <span class="keyword">import</span> loss <span class="keyword">as</span> gloss, nn</span><br><span class="line">net = nn.Sequential()</span><br><span class="line">net.add(nn.Dense(<span class="number">256</span>,activation = <span class="string">'relu'</span>),nn.Dense(<span class="number">10</span>))</span><br><span class="line">net.initialize(init.Normal(sigma = <span class="number">0.01</span>))</span><br></pre></td></tr></table></figure>

<p>2、使用与softmax回归几乎相同的步骤来读取数据并训练模型,学习率为0.5</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">batch_size = <span class="number">256</span> <span class="comment">#批量大小设置</span></span><br><span class="line">train_iter,test_iter = d2l.load_data_fashion_mnist(batch_size)</span><br><span class="line">oss = gloss.SoftmaxCrossEntropyLoss()</span><br><span class="line">trainer = gluon.Trainer(net.collect_params()，<span class="string">'sgd'</span>,&#123;<span class="string">'learining_rate'</span>:<span class="number">0.5</span>&#125;)</span><br><span class="line">num_epochs = <span class="number">5</span> <span class="comment">#迭代周期num_epochs指的是要循环学习几次</span></span><br><span class="line">d2l.train_ch3(net,train_iter,test_iter,loss,num_epochs,batch_size,<span class="literal">None</span>,<span class="literal">None</span>,trainer)</span><br></pre></td></tr></table></figure>



<h2 id="5、模型选择与拟合问题"><a href="#5、模型选择与拟合问题" class="headerlink" title="5、模型选择与拟合问题"></a>5、模型选择与拟合问题</h2><p>训练误差：模型在训练集上表现出来的误差；</p>
<p>泛化误差：任意一个测试数据样本上表现出的误差的期望；</p>
<p>使用验证数据集来进行模型选择：预留一部分在训练、测试数据集之外的数据来进行模型选择。K折交叉验证：将原始数据分成K个不重合的子数据集，做K次模型训练和验证，每一次用一个子数据集来验证模型，其他用于训练模型。最后对这K次结果分别求平均。</p>
<p>欠拟合：模型无法得到较低的训练误差。</p>
<p>过拟合：模型训练误差远小于其在测试集上误差；模型越复杂、训练集越小越容易过拟合。</p>
<h2 id="6、权重衰减、丢弃法来处理过拟合"><a href="#6、权重衰减、丢弃法来处理过拟合" class="headerlink" title="6、权重衰减、丢弃法来处理过拟合"></a>6、权重衰减、丢弃法来处理过拟合</h2><p>权重衰减：等价于L2范数正则化，通过为模型损失函数添加惩罚项，使学到的模型参数值较小。</p>
<p>L2惩罚项指的是：模型权重参数的每一个元素的平方和与一个正的常数的乘积。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">l2_penalty</span><span class="params">(w)</span>:</span></span><br><span class="line">  <span class="keyword">return</span> (w**<span class="number">2</span>).sum() / <span class="number">2</span></span><br></pre></td></tr></table></figure>

<h3 id="权重衰减Gluon简洁实现："><a href="#权重衰减Gluon简洁实现：" class="headerlink" title="权重衰减Gluon简洁实现："></a>权重衰减Gluon简洁实现：</h3><p>构造Trainer实例时通过wd参数来指定权重衰减超参数，默认下会对权重、偏差同时衰减。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#对权重参数衰减，权重名称一般以weight结尾</span></span><br><span class="line">trainer_w = gluon.Trainer(net.collect_params(<span class="string">'.*weight'</span>),<span class="string">'sgd'</span>,&#123;<span class="string">'learning_rate'</span>:lr,<span class="string">'wd'</span>:wd&#125;)</span><br><span class="line"><span class="comment">#不对偏差参数衰减，偏差名称一般以bias结尾</span></span><br><span class="line">trainer_b = gluon.Trainer(net.collect_params(<span class="string">'.*bias'</span>),<span class="string">'sgd'</span>,&#123;<span class="string">'learning_rate'</span>:lr&#125;)</span><br></pre></td></tr></table></figure>



<p>丢弃法：隐藏单元有一定的概率P被丢弃掉，丢弃概率是丢弃法的超参数。具体而言，随机变量$为0和1的概率分别为P和1-P。</p>
<p>定义dropout函数，以drop_prob的概率丢弃NDArray输入X中的元素。</p>
<h3 id="丢弃法Gluon简洁实现："><a href="#丢弃法Gluon简洁实现：" class="headerlink" title="丢弃法Gluon简洁实现："></a>丢弃法Gluon简洁实现：</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">net = nn.Sequential()</span><br><span class="line">net.add(nn.Dense(<span class="number">256</span>,activation = <span class="string">'relu'</span>),nn.Dropout(drop_prob1),  <span class="comment">#在第一个全连接层后添加丢弃层</span></span><br><span class="line">       nn.Dense(<span class="number">256</span>,activation = <span class="string">'relu'</span>),nn.Dropout(drop_prob2),nn.Dense(<span class="number">10</span>))</span><br><span class="line">net.initialize(init.Normal(sigma = <span class="number">0.01</span>))</span><br></pre></td></tr></table></figure>



<h2 id="7、反向传播"><a href="#7、反向传播" class="headerlink" title="7、反向传播"></a>7、反向传播</h2><p>反向传播：指计算神经网络梯度的方法，依据链式法则，其梯度计算可能依据各变量的当前值，而这些变量的当前值是通过正向传播计算得到的。</p>
<p>正向传播的计算可能依赖于模型参数的当前值，而参数是在反向传播的梯度计算后通过优化算法迭代的。</p>
<p>模型参数初始化完成后，交替地进行正向传播和反向传播，并根据反向传播计算的梯度迭代模型参数。</p>
<p>梯度衰减、爆炸：由于层数过大时，输出呈幂次爆炸增长，故梯度爆炸或梯度消失。</p>
<h2 id="8、深度学习计算原理细节"><a href="#8、深度学习计算原理细节" class="headerlink" title="8、深度学习计算原理细节"></a>8、深度学习计算原理细节</h2><h3 id="1、基于Block类的模型构建"><a href="#1、基于Block类的模型构建" class="headerlink" title="1、基于Block类的模型构建"></a>1、基于Block类的模型构建</h3><p>Block类是nn模块里提供的一个模型构造类，继承Block类来构造多层感知机，重载init函数与forward函数，分别用于创建模型参数与定义前向计算。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MLP</span><span class="params">(nn.Block)</span>:</span> <span class="comment">#声明带有模型参数的层，声明2个全连接层</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self, **kwargs)</span>:</span></span><br><span class="line">        <span class="comment">#调用父类构造函数来进行必要初始化。</span></span><br><span class="line">        super(MLP,self).__init__(**kwargs)</span><br><span class="line">        self.hidden = nn.Dense(<span class="number">256</span>, activation=<span class="string">'relu'</span>) <span class="comment">#隐藏层</span></span><br><span class="line">        self.output = nn.Dense(<span class="number">10</span>) <span class="comment">#输出层</span></span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">forward</span><span class="params">(self,x)</span>:</span> <span class="comment">#定义模型的前向计算，即如何根据输入x计算返回所需的模型输出</span></span><br><span class="line">        <span class="keyword">return</span> self.output(self.hidden(x))</span><br></pre></td></tr></table></figure>

<p>无需定义反向传播，系统将自动求梯度而生成反向传播所需的backward函数</p>
<p>实例化MLP类得到net，并传入输入数据X并做一次前向计算。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">X = nd.random.uniform(shape=(<span class="number">2</span>,<span class="number">20</span>))</span><br><span class="line">net = MLP()</span><br><span class="line">net.initialize()</span><br><span class="line">net(X)</span><br></pre></td></tr></table></figure>

<h3 id="2、构建一个继承于Block类的继承类"><a href="#2、构建一个继承于Block类的继承类" class="headerlink" title="2、构建一个继承于Block类的继承类"></a>2、构建一个继承于Block类的继承类</h3><p>提供add函数来逐一添加串联的Block子类实例，而模型的前向计算就是将这些实例按顺序逐一计算。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MySequential</span><span class="params">(nn.Block)</span>:</span></span><br><span class="line">    def__init__(self,**kwargs):</span><br><span class="line">        super(MySequential,self).__init__(**kwargs)</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">add</span><span class="params">(self,block)</span>:</span></span><br><span class="line">        <span class="comment">#block为Block实例，当MySequential实例调用initialize函数时，系统会自动对其所有成员初始化</span></span><br><span class="line">        self._children[block.name] = block</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">forward</span><span class="params">(self,x)</span>:</span></span><br><span class="line">        <span class="comment">#OrderedDict保证按添加顺序遍历成员</span></span><br><span class="line">        <span class="keyword">for</span> block <span class="keyword">in</span> self._children.values():</span><br><span class="line">            x = block(x)</span><br><span class="line">         <span class="keyword">return</span> x</span><br></pre></td></tr></table></figure>



<h3 id="3、自定义初始化模型参数"><a href="#3、自定义初始化模型参数" class="headerlink" title="3、自定义初始化模型参数"></a>3、自定义初始化模型参数</h3><p>对于Sequential类构造的神经网络，可通过方括号[]来访问网络的任一层。同时Sequential实例中含模型参数的层，可通过Block类的params属性来访问该层包含的参数。</p>
<p>共享模型参数：在利用Block类中的forward函数里多次调用一个层来计算。或者，在构造层时指定特定的参数，若不同层使用同一份参数，则它们会在前向、反向时均共享相同的参数。</p>
<p>延后初始化：只有将形状是（，）的输入X传进网络做前向计算net(X)时，系统才能推断出该层权重参数形状为（，），此时才能真正地开始初始化参数。</p>
<p>避免延后初始化：1、要对已初始化的模型重新初始化时，由于参数形状不会变化，能够立即重新初始化；2、创建层的时候指定了它的输入个数，故系统不需要额外信息来推测参数形状。</p>
<h3 id="4、自定义层"><a href="#4、自定义层" class="headerlink" title="4、自定义层"></a>4、自定义层</h3><h3 id="5、读取与存储"><a href="#5、读取与存储" class="headerlink" title="5、读取与存储"></a>5、读取与存储</h3><p>将内存中训练好的模型参数存储在硬盘上，供后续读取使用。</p>

    </div>
    
    
    
    
    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>
        <div class="reward-container">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/alipay.png" alt="Li Yudong 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Li Yudong
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://example.com/2020/08/19/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E5%88%9D%E8%A7%81/" title="MXNET机器学习初见">http://example.com/2020/08/19/机器学习/机器学习初见/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

    
      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E5%88%9D%E5%AD%A6/" rel="tag"># 机器学习初学</a>
          </div>
    
        

    
        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/08/19/C++/C-%E5%9B%9B%E5%A4%A9%E5%85%A5%E9%97%A8/" rel="prev" title="C++初见">
      <i class="fa fa-chevron-left"></i> C++初见
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/08/20/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/%E5%8D%B7%E7%A7%AF%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/" rel="next" title="卷积神经网络">
      卷积神经网络 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1、基础知识"><span class="nav-number">1.</span> <span class="nav-text">1、基础知识</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2、线性回归"><span class="nav-number">2.</span> <span class="nav-text">2、线性回归</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Gluon简洁实现："><span class="nav-number">2.1.</span> <span class="nav-text">Gluon简洁实现：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3、softmax回归"><span class="nav-number">3.</span> <span class="nav-text">3、softmax回归</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Gluon简洁实现"><span class="nav-number">3.1.</span> <span class="nav-text">Gluon简洁实现</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4、多层感知机"><span class="nav-number">4.</span> <span class="nav-text">4、多层感知机</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Gluon的简洁实现"><span class="nav-number">4.1.</span> <span class="nav-text">Gluon的简洁实现</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5、模型选择与拟合问题"><span class="nav-number">5.</span> <span class="nav-text">5、模型选择与拟合问题</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6、权重衰减、丢弃法来处理过拟合"><span class="nav-number">6.</span> <span class="nav-text">6、权重衰减、丢弃法来处理过拟合</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#权重衰减Gluon简洁实现："><span class="nav-number">6.1.</span> <span class="nav-text">权重衰减Gluon简洁实现：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#丢弃法Gluon简洁实现："><span class="nav-number">6.2.</span> <span class="nav-text">丢弃法Gluon简洁实现：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7、反向传播"><span class="nav-number">7.</span> <span class="nav-text">7、反向传播</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8、深度学习计算原理细节"><span class="nav-number">8.</span> <span class="nav-text">8、深度学习计算原理细节</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1、基于Block类的模型构建"><span class="nav-number">8.1.</span> <span class="nav-text">1、基于Block类的模型构建</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2、构建一个继承于Block类的继承类"><span class="nav-number">8.2.</span> <span class="nav-text">2、构建一个继承于Block类的继承类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3、自定义初始化模型参数"><span class="nav-number">8.3.</span> <span class="nav-text">3、自定义初始化模型参数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4、自定义层"><span class="nav-number">8.4.</span> <span class="nav-text">4、自定义层</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5、读取与存储"><span class="nav-number">8.5.</span> <span class="nav-text">5、读取与存储</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Li Yudong"
      src="/images/author.jpg">
  <p class="site-author-name" itemprop="name">Li Yudong</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">95</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/tiarmor1" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;tiarmor1" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1157019137@qq.com" title="E-Mail → mailto:1157019137@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2020 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Li Yudong</span>
</div>

        








      </div>
    </footer>
  </div>

  
  
  <script color='0,0,0' opacity='0.5' zIndex='-1' count='150' src="/lib1/canvas-nest/canvas-nest.min.js"></script>
  <script src="/lib1/anime.min.js"></script>
  <script src="/lib1/velocity/velocity.min.js"></script>
  <script src="/lib1/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

  
   <canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;" ></canvas> 
   <script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
   <script type="text/javascript" src="/js/fireworks.js"></script>
  
</body>
</html>

